package com.bayss.core.service;

import com.bayss.core.entity.bak.BakPic;
import com.bayss.core.entity.bak.BakPicExample;
import com.bayss.core.entity.bak.mapper.BakPicMapper;
import com.bayss.core.model.ShowBak;
import com.bayss.core.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BakService {

    private BakPicMapper bakPicMapper;

    @Autowired
    public BakService(BakPicMapper bakPicMapper){
        this.bakPicMapper = bakPicMapper;
    }

    public void saveBak(String key, String folder){
        BakPic bakPic = BakPic.builder()
                .folder(folder)
                .ossKey(key)
                .status(0)
                .build();
        bakPicMapper.insertSelective(bakPic);
    }

    public List<ShowBak> getList(int maxId, int count) {
        BakPicExample bakPicExample = new BakPicExample();
        bakPicExample.setOrderByClause("id asc limit " + count);
        BakPicExample.Criteria criteria = bakPicExample.createCriteria();
        criteria.andStatusEqualTo(0);
        criteria.andIdGreaterThan(maxId);
        List<BakPic> bakPics = bakPicMapper.selectByExample(bakPicExample);
        return bakPics.stream()
                .map(bakPic -> ShowBak.builder()
                .id(bakPic.getId())
                .folder(bakPic.getFolder())
                .intime(bakPic.getIntime())
                .url(FileUtil.imgUrl(bakPic.getOssKey())).build())
                .collect(Collectors.toList());
    }

    public void submitBak(int id){
        BakPic bakPic = bakPicMapper.selectByPrimaryKey(id);
        bakPic.setStatus(1);
        bakPicMapper.updateByPrimaryKey(bakPic);
    }



}
